package edu.njucm.herbiv.controller

import edu.njucm.herbiv.utils.annotation.LogRequest
import edu.njucm.herbiv.model.*
import edu.njucm.herbiv.pojo.entity.AnalysisStatus
import edu.njucm.herbiv.pojo.entity.FunctionType
import edu.njucm.herbiv.service.HerbivService
import edu.njucm.herbiv.utils.FileUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * @Author ZhouHang
 * @Date 24/5/16 周四
 */
@RestController
@RequestMapping("/api/herbiv")
class HerbivController @Autowired constructor (
    val herbivService: HerbivService
){
    @PostMapping("/from_tcm")
    @LogRequest
    fun fromTcm(@RequestBody req: FromTcmModel): MessageModel  {
        val analysisNo = req.getAnalysisNo()
        herbivService.fromTcm(req, analysisNo)
        return MessageModel(2000, analysisNo)
    }

    @PostMapping("/from_formula")
    @LogRequest
    fun fromFormula(@RequestBody req: FromFormulaModel): MessageModel  {
        val analysisNo = req.getAnalysisNo()
        herbivService.fromFormula(req, analysisNo)
        return MessageModel(2000, analysisNo)
    }

    @PostMapping("/from_tcm_protein")
    @LogRequest
    fun fromTcmProtein(@RequestBody req: FromTcmProteinModel): MessageModel  {
        val analysisNo = req.getAnalysisNo()
        herbivService.fromTcmProtein(req, analysisNo)
        return MessageModel(2000, analysisNo)
    }

    @LogRequest
    @PostMapping("/from_formula_protein")
    fun fromFormulaProtein(@RequestBody req: FromFormulaProtein): MessageModel {
        val analysisNo = req.getAnalysisNo()
        herbivService.fromFormulaProtein(req, analysisNo)
        return MessageModel(2000, analysisNo)
    }

    @LogRequest
    @PostMapping("/from_protein")
    fun fromProtein(@RequestBody req: FromProtein): MessageModel {
        val analysisNo = req.getAnalysisNo()
        herbivService.fromProtein(req, analysisNo)
        return MessageModel(2000, analysisNo)
    }

    @LogRequest
    @PostMapping("/get_result")
    fun getResult(@RequestParam("analysis_no") analysisNo: String): ResultModel {
        // 查询失败
        val order = herbivService.getResult(analysisNo)
            ?: return ResultModel(
                code = 3000,
                msg = "请输入正确的分析号",
            )
        when (order.status) {
            AnalysisStatus.SUCCESS -> return ResultModel(
                code = 2000,
                msg = "获取结果成功",
                resultReq = herbivService.getFileUri(analysisNo, FileUtils.RESULTSNAME),
                echartReq = herbivService.getFileUri(analysisNo, FileUtils.GRAPHNAME),
                cytoReq = herbivService.getFileUri(analysisNo, FileUtils.NETWORKNAME),
                cytoTypeReq = herbivService.getFileUri(analysisNo, FileUtils.TYPENAME),
            )
            AnalysisStatus.ANALYSING -> return ResultModel(
                code = 3000,
                msg = "正在分析中，请稍后"
            )
            AnalysisStatus.PROCESS_FAIL -> return ResultModel(
                code = 4000,
                msg = "当前分析因服务器资源问题退出，适当修改 Score 或联系管理员"
            )
            else -> return ResultModel(
                code = 5000,
                msg = "Status 变成了一个奇怪的值，原因未知"
            )
        }
    }

    @LogRequest
    @GetMapping("/get_statistic")
    fun getStatistic(): MessageModel {
        return MessageModel(
            code = 2000,
            msg = mapOf(
                "fromTcm" to herbivService.getFunctionCount(FunctionType.FROM_TCM),
                "fromFormula" to herbivService.getFunctionCount(FunctionType.FROM_FORMULA),
                "fromTcmProtein" to herbivService.getFunctionCount(FunctionType.FROM_TCM_PROTEIN),
                "fromFormulaProtein" to herbivService.getFunctionCount(FunctionType.FROM_FORMULA_PROTEIN),
                "fromProtein" to herbivService.getFunctionCount(FunctionType.FROM_PROTEIN)
            )
        )
    }
}